MSimport edge_tts
import asyncio
import aiofiles
import wave
import warnings
warnings.filterwarnings("ignore")

async def text_to_speech_direct(text, filename, proxy_url="http://10.102.10.197:3031", voice="zh-CN-YunxiNeural"):
    try:
        # 创建语音对象
        communicate = edge_tts.Communicate(
            text=text,
            voice=voice,
            proxy=proxy_url
        )
        
        # 直接写入WAV文件
        async with aiofiles.open(filename, "wb") as wav_file:
            # 创建WAV文件头
            with wave.open(wav_file, 'wb') as wav:
                wav.setnchannels(1)  # 单声道
                wav.setsampwidth(2)   # 16位采样
                wav.setframerate(24000)  # 24kHz采样率
                
                # 流式处理音频数据
                async for chunk in communicate.stream():
                    if chunk["type"] == "audio":
                        await wav_file.write(chunk["data"])
        
        print(f"✅ 语音文件已成功保存为WAV: {filename}")
        return True
    
    except Exception as e:
        print(f"❌ 转换失败: {str(e)}")
        return False

asyncio.run(text_to_speech_direct('你好呀，我喜欢你','a1.wav'))
